//
//  CityChoose.m
//  CityChoose
//
//  Created by apple on 17/2/6.
//  Copyright © 2017年 desn. All rights reserved.
//

#import "YDCityChooseView.h"

#import "YDProvinceModel.h"


static CGFloat cityPickViewHeigh = 200;
static CGFloat toolsViewHeith = 40;
static CGFloat animationTime = 0.25;


@interface YDCityChooseView()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, weak) UIPickerView *cityPickerView;/** 城市选择器 */
@property (nonatomic, weak) UIButton *sureButton;        /** 确认按钮 */
@property (nonatomic, weak) UIButton *canselButton;      /** 取消按钮 */
@property (nonatomic, weak) UIView *toolsView;           /** 自定义标签栏 */
@property (nonatomic, weak) UIView *bgView;              /** 背景view */

@property (nonatomic, strong) UILabel *chooseNamelbl;

//当前选中的模型
@property (nonatomic , copy) NSString *currentChoseName;

@property (nonatomic, assign)CGFloat bgViewHeith;


@end

@implementation YDCityChooseView

-(void)setModelArray:(NSMutableArray *)modelArray
{
    _modelArray = modelArray;
    [self.cityPickerView reloadComponent:1];
    
    //刷新
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _bgViewHeith =  isPhoneX ?  240 + 34 :240;
        [self initSubViews];
        
    }
    return self;
}

- (void)initSubViews
{
    
    self.frame = [UIApplication sharedApplication].keyWindow.bounds;
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
   
    [self bgView];
    [self toolsView];
    [self canselButton];
    [self sureButton];
    [self cityPickerView];
    [self showPickView];
    
}
+(instancetype)cityChooseViewWithDataModelArray:(NSMutableArray *)modelArray
{
    YDCityChooseView *chooseView = [[self alloc] init];
    chooseView.modelArray = modelArray;
    return chooseView;
}



#pragma event menthods
- (void)canselButtonClick
{
    [self hidePickView];

}

- (void)sureButtonClick
{
    [self hidePickView];
    
    //点击确认回调
    if (self.config) {
        self.config(self.currentChoseName,YES);
    }
}

#pragma mark private methods
- (void)showPickView
{
    [UIView animateWithDuration:animationTime animations:^{
        self.bgView.frame = CGRectMake(0, self.frame.size.height - _bgViewHeith, self.frame.size.width, _bgViewHeith);
    } completion:^(BOOL finished) {
        
    }];
}


- (void)hidePickView
{
    [UIView animateWithDuration:animationTime animations:^{
        self.bgView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, _bgViewHeith);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

}


#pragma mark - pickerViewDatasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.modelArray.count;
}

#pragma mark - pickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, self.frame.size.width - 40, 30)];
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [self.modelArray objectAtIndex:row];

    if (row == 0 && !_currentTextFieldName.length) {
        self.currentChoseName = label.text;
        if (self.config) {
            self.config(label.text, YES);
            
        }
    }
   
    
    return label;
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    //滑动到某个row不用回调
    self.currentChoseName  = self.modelArray[row];
//    if (self.config) {
//        self.config(self.currentChoseName,NO);
//    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    if ([touches.anyObject.view isKindOfClass:[self class]]) {
        [self hidePickView];
    }
}
-(void)setChoseTypeName:(NSString *)choseTypeName
{
    _choseTypeName = choseTypeName;
    self.chooseNamelbl.text = choseTypeName;
}

#pragma mark - lazy 

- (UIView *)bgView
{
    if (_bgView == nil) {
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, _bgViewHeith)];
        [self addSubview:bgView];
        bgView.backgroundColor = [UIColor whiteColor];
        _bgView = bgView;
    }
    return _bgView;
}

- (UIPickerView *)cityPickerView
{
    if (_cityPickerView == nil) {
        UIPickerView *cityPickerView = ({
            UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, cityPickViewHeigh)];
            pickerView.backgroundColor = [UIColor whiteColor];
            [pickerView setShowsSelectionIndicator:YES];
            pickerView.delegate = self;
            pickerView.dataSource = self;
            pickerView;
        });
        [self.bgView addSubview:cityPickerView];
        
        _cityPickerView = cityPickerView;
    }
    return _cityPickerView;
}

- (UIView *)toolsView
{
    if (_toolsView == nil) {
        
        CGFloat toolY = isPhoneX ? _bgViewHeith - (toolsViewHeith + 34) :_bgViewHeith - toolsViewHeith;
        
        UIView *toolsView = [[UIView alloc] initWithFrame:CGRectMake(0, toolY, self.frame.size.width, toolsViewHeith)];
        toolsView.layer.borderWidth = 0.5;
        toolsView.layer.borderColor = [UIColor grayColor].CGColor;
        
        
        self.chooseNamelbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, (LKScreenW - 2*80), toolsViewHeith)];
        self.chooseNamelbl.textColor = [UIColor colorWithHexString:dark];
        self.chooseNamelbl.font = [UIFont systemFontOfSize:16];
        self.chooseNamelbl.textAlignment = NSTextAlignmentCenter;
        [toolsView addSubview:self.chooseNamelbl];

        [self.bgView addSubview:toolsView];
        _toolsView = toolsView;
    }
    return _toolsView;
}

- (UIButton *)canselButton
{
    if (_canselButton == nil) {
        UIButton *canselButton = ({
            UIButton *canselButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 0, 50, toolsViewHeith)];
            [canselButton setTitle:@"取消" forState:UIControlStateNormal];
            [canselButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [canselButton addTarget:self action:@selector(canselButtonClick) forControlEvents:UIControlEventTouchUpInside];
            canselButton;
        });
        [self.toolsView addSubview:canselButton];
        
        _canselButton = canselButton;
    }
    return _canselButton;
}

- (UIButton *)sureButton
{
    if (_sureButton == nil) {
        UIButton *sureButton = ({
            UIButton *sureButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 20 - 50, 0, 50, toolsViewHeith)];
            [sureButton setTitle:@"确定" forState:UIControlStateNormal];
            [sureButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
            sureButton;
        });
        
        [self.toolsView addSubview:sureButton];
        
        _sureButton = sureButton;
    }
    return _sureButton;
}

@end
